<template>
  <div class="network_assign_rule">
    <div v-show="value" class="header">
      <div class="btn_group">
        <HPlainButton @click="addRule"> 新建分配规则 </HPlainButton>
      </div>
      <div class="search">
        <el-input
          v-model.trim="name"
          placeholder="名称搜索"
          clearable
          @keyup.enter="onShowUpgrade"
          @clear="onShowUpgrade"
        />
        <HButton type="primary" @click="onShowUpgrade">
          <el-icon>
            <Search />
          </el-icon>
          搜索
        </HButton>
      </div>
    </div>
    <div v-show="value" class="table_container">
      <el-table
        ref="tableDom"
        border
        stripe
        resizable
        :data="tableData"
        height="100%"
        @selection-change="handleChange"
      >
        <el-table-column show-overflow-tooltip type="selection" width="40" fixed="left" />
        <el-table-column
          show-overflow-tooltip
          type="index"
          :index="indexMethod"
          label="序号"
          width="50"
        />
        <el-table-column show-overflow-tooltip prop="name" label="规则名称" min-width="200" />
        <el-table-column show-overflow-tooltip prop="status" label="状态" width="70">
          <template #default="scope">
            <HStatus v-if="!scope.row.status">启用</HStatus>
            <HStatus v-if="scope.row.status" type="info">禁用</HStatus>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="moduleName" label="模块名称" width="250" />
        <el-table-column show-overflow-tooltip prop="condition" label="规则条件" width="150" />
        <el-table-column show-overflow-tooltip prop="type" label="分配方式" width="80">
          <template #default="scope">
            <span v-if="scope.row.type == 1">平均分配</span>
            <span v-if="scope.row.type == 2">随机分配</span>
            <span v-if="scope.row.type == 3">熟客优先</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="empType" label="人员类型" width="80">
          <template #default="scope">
            <span v-if="scope.row.empType == 1">客服</span>
            <span v-if="scope.row.empType == 2">客服组</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="emp" label="分配人员" width="250" />
        <el-table-column show-overflow-tooltip prop="weight" label="优先级" width="70" />
        <el-table-column show-overflow-tooltip prop="createTime" label="创建时间" width="150" />
        <el-table-column show-overflow-tooltip prop="updateTime" label="更新时间" width="150" />
        <el-table-column label="操作" width="60" fixed="right">
          <template #default="scope">
            <HLink @click="onShowUpgrade">详细</HLink>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-show="value" class="pagination">
      <Pagination
        ref="pagination"
        :list_total="total"
        @size-change="onShowUpgrade"
        @current-change="onShowUpgrade(false)"
      />
    </div>

    <!-- 添加/编辑  规则弹窗 -->
    <h-drawer
      v-model="addDialog"
      :width="800"
      unit="px"
      :title="dialogTitle"
      :loading="dialogLoading"
    >
      <el-form
        ref="formRef"
        label-position="top"
        inline-message
        class="modal_form_content"
        :rules="rules"
        :model="form"
      >
        <el-form-item label="规则名称" class="form-item" prop="name">
          <el-input v-model.trim="form.name" clearable placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="应用范围" prop="module">
          <el-select
            v-model="form.module"
            class="item-select"
            placeholder="请选择应用范围"
            multiple
            collapse-tags
            collapse-tags-tooltip
            filterable
            clearable
            remote
            reserve-keyword
            remote-show-suffix
          >
          </el-select>
        </el-form-item>
        <div class="role_container">
          <div class="title">
            <div class="text">需满足以下所有条件，自动分配规则才会生效</div>
            <h-link @click="addCondition">添加</h-link>
          </div>
          <div class="role_list">
            <template v-for="(item, i) in condition" :key="i">
              <el-form-item class="role_panel">
                <template #label>
                  <div class="role_panel_label">
                    <span class="title">规则{{ ['一', '二', '三', '四'][i] }}</span>
                    <h-link type="error" @click="deleteCondition(i)">删除</h-link>
                  </div>
                </template>
                <div class="role_panel_content">
                  <el-form-item label="规则条件">
                    <el-select v-model="item.cond" class="width-select mb10" filterable clearable>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="规则内容">
                    <el-select v-model="item.value" class="width-select" filterable clearable>
                    </el-select>
                  </el-form-item>
                </div>
              </el-form-item>
            </template>
          </div>
        </div>
        <div class="tips flex-align-center">满足以上条件，则执行以下分配策略</div>
        <el-form-item label="分配方式" class="form-item">
          <el-select
            v-model="form.way"
            class="item-select"
            placeholder="请选择分配方式"
            filterable
            clearable
          >
          </el-select>
        </el-form-item>
        <el-form-item label="分配人员" class="form-item">
          <el-select
            v-model="form.emp"
            class="item-select"
            placeholder="请选择分配人员"
            filterable
            clearable
          >
          </el-select>
        </el-form-item>
        <el-form-item label="优先级" class="form-item">
          <el-input v-model.trim="form.weight" clearable placeholder="不填写则系统默认排序" />
        </el-form-item>
        <el-form-item label="规则状态" class="form-item">
          <el-radio-group v-model="form.status" class="el-group">
            <el-radio :value="false">
              <span class="bg_text">启用</span>
            </el-radio>
            <el-radio :value="true">
              <span class="bg_text">禁用</span>
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <HPlainBgButton v-show="dialogTitle === '编辑规则'" is-modal type="error"
          >删除</HPlainBgButton
        >
        <HButton is-modal type="primary" @click="onShowUpgrade">保存</HButton>
        <HButton is-modal @click="addDialog = false"> 取消 </HButton>
      </template>
    </h-drawer>
  </div>
</template>

<script setup lang="ts">
  import Pagination from '@/components/Pagination/index.vue'
  import { ref, reactive } from 'vue'
  import { Search } from '@element-plus/icons-vue'
  import { ElMessage } from 'element-plus'
  import type { FormInstance } from 'element-plus'

  const value = ref(true)
  const name = ref('') // 名称
  const tableData = ref([]) // 列表数据
  const tableDom = ref() // 列表dom
  const pagination = ref() // 分页dom
  const total = ref(0) // 总数目

  const addDialog = ref(false)
  const dialogTitle = ref('')
  const dialogLoading = ref(false)
  const formRef = ref<FormInstance>() // 表单元素

  // 表单数据
  const form = reactive({
    id: null,
    module: [],
    name: '',
    way: '',
    emp: '',
    status: false,
    weight: '',
  })

  const condition = ref([
    {
      cond: '',
      value: '',
    },
  ])

  // 验证规则
  const rules = reactive({
    name: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
  })

  // 列表序号
  const indexMethod = (index: number) => {
    return index + 1
  }

  // 添加条件
  const addCondition = () => {
    if (condition.value.length == 4) {
      ElMessage({
        message: '最多添加4个条件！',
        type: 'warning',
        duration: 5000,
        plain: true,
        showClose: true,
      })
      return
    }
    condition.value.push({
      cond: '',
      value: '',
    })
  }

  // 删除条件
  const deleteCondition = (i) => {
    if (condition.value.length == 1) {
      ElMessage({
        message: '至于保留1个条件！',
        type: 'warning',
        duration: 5000,
        plain: true,
        showClose: true,
      })
      return
    }
    condition.value.splice(i, 1)
  }

  // 获取表格选中的数据
  const selecttion = ref([])
  const handleChange = (val: any[]) => {
    if (val.length != 0) {
      val.forEach((item: { id: any }) => {
        if (Object.values(selecttion.value).indexOf(item.id) == -1) {
          selecttion.value.push(item.id)
        } else {
          selecttion.value = selecttion.value.filter((i) => i == item.id)
        }
      })
    } else {
      selecttion.value = []
    }
  }

  // 获取列表数据
  const getList = () => {}

  // 添加规则
  const addRule = () => {
    dialogTitle.value = '添加规则'
    addDialog.value = true
    form.id = null
    form.name = ''
    form.status = false
  }

  // 保存方法
  const saveDataMethod = (method, data) => {
    dialogLoading.value = true
    method(data).then((res) => {
      if (res.data.success == true) {
        ElMessage({
          message: res.data.message,
          type: 'success',
          duration: 5000,
          plain: true,
          showClose: true,
        })
        addDialog.value = false
        dialogLoading.value = false
        getList()
      } else {
        ElMessage({
          message: res.data.message,
          type: 'warning',
          duration: 5000,
          plain: true,
          showClose: true,
        })
        dialogLoading.value = false
        return
      }
    })
  }

  // 保存数据
  const saveData = (formEl: FormInstance | undefined) => {
    formEl.validate((valid) => {
      if (valid) {
      }
    })
  }

  const onShowUpgrade = (val = true) => {
    ElMessage({
      type: 'success',
      duration: 5000,
      plain: true,
      showClose: true,
      message: '正在升级维护中，敬请期待...',
    })
  }
</script>

<style scoped lang="scss">
  @import '../../index.scss';
  @import './NetworkRules.scss';
</style>
